package com.ywb.ex09.view;



import com.ywb.ex09.dao.impl.PetDao;
import com.ywb.ex09.model.PetItem;

import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import java.util.ArrayList;


public class PetAdminDialogTest extends PetAdminDialog {
    public PetAdminDialogTest(){
        super();
        queryPetItem();
    }
    @Override
    public void queryPetItem(){
        String[] tablehead=new String[]{"编号","名称","价格（元）","计价单位"};
        PetDao pd=new PetDao();
        ArrayList<PetItem> list=new ArrayList<PetItem>();
        list=pd.queryAllData();

        String[][]tablebody=new String[][]{{"1","波斯猫","852.0","只"}};
        for(PetItem x:list){
            String id=x.getId();
            String petname=x.getPetname();
            double tprice=x.getPrice();
            String price=Double.toString(tprice);
            String unit=x.getUnit();
            String[]temp=new String[]{id,petname,price,unit};
            for(int i=0;i<tablebody.length;i++){
                tablebody[i]=temp;
            }
        }

        TableModel data=new DefaultTableModel(tablebody,tablehead);
        petTable.setModel(data);
    }
    @Override
    public void addPetItem(){
        String id=addIdText.getText().trim();
        String petname=addNameText.getText().trim();
        String price=addPriceText.getText().trim();
        String unit=addUnitText.getText().trim();
        PetItem petItem=new PetItem();
        petItem.setId(id);
        petItem.setPetname(petname);
        petItem.setPrice(Double.valueOf(price));
        petItem.setUnit(unit);
        PetDao petDao=new PetDao();
        petDao.addPetItem(petItem);
    }
    @Override
    public void updatePetItem(){

    }
    @Override
    public void delPetItem(){
        String id=addIdText.getText().trim();
        PetDao petDao=new PetDao();
        petDao.delPetItem(id);
    }
    public static  void main(String[]args){
        new PetAdminDialogTest().setVisible(true);
    }
}
